package com.xsy.android.compose.wandroid.ui.page.artice.list

import android.text.TextUtils
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.xsy.android.compose.wandroid.R
import com.xsy.android.compose.wandroid.logic.model.ArticleModel
import com.xsy.android.compose.wandroid.logic.utils.getHtmlText
import com.zj.banner.utils.ImageLoader

/**
 *@author: xiaosy
 *时间: 2024/7/12
 *@Description: java类作用描述
 *@Version: 1.0
 **/
@Composable
fun ArticleListItem(
    modifier: Modifier = Modifier,
    article: ArticleModel?,
    enterArticle: (urlArgs: ArticleModel) -> Unit
) {
    if (article == null) {
        return
    }
    val hasImage = article.envelopePic.isNotBlank()
    Card(
        modifier = modifier
            .padding(horizontal = 10.dp, vertical = if (hasImage) 8.dp else 6.dp)
            .fillMaxWidth()
            .height(if (hasImage) 90.dp else 70.dp),
        elevation = CardDefaults.cardElevation(defaultElevation = 6.dp),
        shape = RoundedCornerShape(10.dp)
    ) {
        Row(
            modifier = Modifier
                .background(
                    brush = Brush.linearGradient(
                        arrayListOf(
                            colorResource(id = R.color.article_item_bg_start),
                            colorResource(id = R.color.article_item_bg_start),
                            colorResource(id = R.color.article_item_bg_start),
                        )
                    )
                )
                .clickable {
                    enterArticle(article)
                }
        ) {
            if (hasImage) {
                ImageLoader(
                    data = article.envelopePic,
                    modifier = Modifier.aspectRatio(1f)
                )
            }
            Column(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(10.dp)
            ) {
                Text(
                    text = getHtmlText(article.title),
                    style = MaterialTheme.typography.titleMedium,
                    maxLines = if (hasImage) 2 else 1,
                    overflow = TextOverflow.Ellipsis,
                    modifier = Modifier
                        .weight(1f)
                        .wrapContentHeight(Alignment.Top)

                )
                Row(
                    verticalAlignment = Alignment.CenterVertically,
                    modifier = Modifier
                        .weight(1f)
                        .wrapContentHeight(Alignment.Bottom)
                ) {
                    Text(
                        text = article.superChapterName,
                        style = MaterialTheme.typography.titleSmall,
                        maxLines = 1,
                        overflow = TextOverflow.Ellipsis,
                        modifier = Modifier
                            .weight(1f)
                            .wrapContentWidth(Alignment.Start)
                    )
                    Text(
                        text = if (TextUtils.isEmpty(article.author)) article.shareUser else article.author,
                        style = MaterialTheme.typography.titleSmall,
                        maxLines = 1,
                        overflow = TextOverflow.Ellipsis,
                        modifier = Modifier
                            .padding(start = 8.dp)
                            .weight(1f)
                            .wrapContentWidth(Alignment.Start)
                    )
                }

            }
        }
    }

}

@Preview(showBackground = true)
@Composable
private fun PreviewArticleListItem() {
    ArticleListItem(
        article = ArticleModel(title = "标题", superChapterName = "分类", author = "作者")
    ) {

    }
}



